<?php
namespace cmf\controller;
use cmf\controller\PluginRestBaseController;
use cmf\lib\interfaceauth\InterfaceAuth;
use cmf\lib\interfaceauth\Rsa;
use think\Model;
use think\Config;

//插件接口控制Api器基类
class ApiUserPluginBaseController extends PluginRestBaseController
{
    protected $param ; 
    private $identity;
    // 获取token 
    public function _initialize()
    {
        parent::_initialize();
        $this->param = $this->request->param();
        if( isset( $this->param [ 'rsadata' ] ) ){
            //如果使用rsa加密数据,则解密
            $this->param = parent::publicDecrypt( $this->param [ 'rsadata' ] );
        }
        /*未开启
        if( isset( $this->param [ 'aesdata' ] ) ){
            //如果使用aes加密数据,则解密
            $this->param = parent::publicDecrypt( $this->param [ 'aesdata' ] );
        }*/
        
        // 调试模式开启 并且 在白名单中 允许免验证调用接口
        if( !isset( $this->param[ 'tt' ] ) ){
            //调试模式开启 并且 在白名单中 允许免验证调用接口
            if( isset( $this->param[ 'need_verify' ] ) ||  !(  APP_DEBUG && in_array( get_client_ip() , Config::get( 'allow_client_ips' )  ) ) ){
                $this->start();
            }
        }
        
    }
    
    /**
     * 开始方法
     **/
    private function start()
    {
        if( !isset( $this->param[ 'proof' ] ) || empty( $this->param [ 'proof' ] ) ){
           return api_json_echo( false , '无效的请求' , $this->param , 10001 );
        }
        $this->checkProof();
    }
    
    /*
    *验证凭证信息
    **/
    private function checkProof()
    {
        $res = InterfaceAuth::verificationToken( $this->param [ 'proof' ] );
       
        $isRefresh =  ( isset( $this->param [ 'refresh' ] ) && $this->param[ 'refresh' ] == "20200707" );
        if( is_array( $res ) &&  isset( $res['status'] ) && $res [ 'status' ] == false ){
            //如果 刷新token  或者是   (过期了,并且是刷新token) 操作,则先不反回结果,
            $isRefresh = ( $isRefresh || ( $isRefresh  && $res [ 'code' ] == '20014' ) );
            if( !$isRefresh ){
                return api_json_echo( false , $res[ 'message' ]  , null , $res [ 'code' ]  );
            }
        }

        if( !isset( $res -> uid ) || empty( $res ->uid ) ){
            return api_json_echo( false , '用户不存在,请检查' , null , 30001 );
        }
        try {
            $info = Model('\\app\\admin\\model\\UserModel')->where('id',$res->uid )->find();
        } catch ( \Exception $e ) {
            throw new \Exception( $e->getMessage() );
        }
        if( empty( $info ) ){
            return api_json_echo( false , '用户不存在,请检查' , null , 30001 );
        }
        
        $this->identity = $res;
        //刷新token
        if( $isRefresh ){
            $this->refreshToken();   
        }
    }
    
    
    /**
     * 
     * 获取标识信息
     **/
    protected function getIdentity()
    {
        return $this->identity;
    }
    
    
    /** 
     * 刷新token
     **/
    public function refreshToken()
    {
        //检查刷新token是否有效
         if( !isset( $this->param[ 'refresh_token' ] ) || empty( $this->param[ 'refresh_token' ] ) ){
            return api_json_echo( false , 'refresh_token不存在' , null , 20018 );
        }
        
        $refreshResult = InterfaceAuth::verificationToken( $this->param [ 'refresh_token' ] );
        if( is_array( $refreshResult ) && isset( $refreshResult['status'] ) && $refreshResult [ 'status' ] == false ){
            if( $refreshResult [ 'code' ] == '20014' ){
                $refreshResult [ 'code' ] = '20019';
                $refreshResult[ 'message' ] = 'refresh_token已过期'; 
            }
            return api_json_echo( false , $refreshResult[ 'message' ]  , null , $refreshResult [ 'code' ]  );
        }
        
        if( !isset( $refreshResult -> uid ) || empty( $refreshResult ->uid ) &&  $refreshResult ->uid != $this->identity->uid ){
            return api_json_echo( false , '用户不存在,请检查' , null , 30001 );
        }
        
        //执行刷新操作
        $token = InterfaceAuth::createToken( $this->identity );
        return api_json_echo( true, 'refresh success' , [ 'token' => $token ] );
    }
    
    
    
    

}